package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-08-28 16:34
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 根据三级分类ID查询包含平台属性列表（包含属性值）
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //1.获取持久层mpper
        //调用mapper动态SQL
        return baseMapper.getAttrInfoList(category1Id, category2Id, category3Id);
    }

    /**
     * 平台属性新增或者修改
     *
     * @param baseAttrInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)  //默认情况下抛出运行时异常RuntimException才会进行是事务回滚
    public void saveorUpdateAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1.判断提交入参对象中ID为空-新增逻辑
        if (baseAttrInfo.getId() == null) {
            //1.1 先获取到平台属性对象（前端提交参数直接满足后端保存要求）直接保存即可
            this.save(baseAttrInfo);
            // TODO 新增后采用主键回填<selectKey> 查询最后一条新增数据id </selectKey>
            Long attrId = baseAttrInfo.getId();

            //1.2 获取到平台属性中属性值集合 循环为每个属性值关联平台属性 attrId 批量保存
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(attrValue -> {
                    //关联平台属性ID
                    attrValue.setAttrId(attrId);
                });
                //批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        } else {
            //2.判断提交入参对象中ID有值-修改逻辑
            //2.1 根据ID修改平台属性信息
            this.updateById(baseAttrInfo);

            //2.2 修改平台属性值
            //2.2.1 根据平台属性ID删除平台属性值
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);

            //2.2.2 将提交平台属性值进行批量保存 关联平台属性ID
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach(attrValue -> {
                    //关联平台属性ID
                    attrValue.setAttrId(baseAttrInfo.getId());
                });
                //批量保存
                baseAttrValueService.saveBatch(attrValueList);
            }
        }
    }

    /**
     * 根据平台属性Id 获取到平台属性值集合
     *
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId, attrId);
        return baseAttrValueService.list(queryWrapper);
    }
}
